package com.eboss2.sbs.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.allen.library.SuperButton;
import com.eboss2.sbs.R;
import com.eboss2.sbs.activity.base.BaseActivity;
import com.eboss2.sbs.adapter.ServiceOrderListAdapter;
import com.eboss2.sbs.config.Config;
import com.eboss2.sbs.http.HttpUtil;
import com.eboss2.sbs.model.ServiceGoodsOrderInfo;
import com.eboss2.sbs.weight.DialogUtils;
import com.loopj.android.http.RequestParams;
import com.mytools.tool.activityManager.MyActivityManager;
import com.mytools.tool.utils.TimeUtil;
import com.mytools.tool.views.MyDialog;
import com.tool.utils.utils.StringUtils;
import com.tool.utils.view.MyListView;
import com.tool.utils.view.RTextView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ActivityServiceGoodsOrderDetail extends BaseActivity {


    @BindView(R.id.titleback_btn_back)
    LinearLayout titlebackBtnBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_order_no)
    TextView tvOrderNo;
    @BindView(R.id.tv_order_time)
    TextView tvOrderTime;
    @BindView(R.id.listview_spec)
    MyListView listviewSpec;
    @BindView(R.id.ll_spec)
    LinearLayout llSpec;
    @BindView(R.id.tv_order_num)
    TextView tvOrderNum;
    @BindView(R.id.tv_order_amt)
    TextView tvOrderAmt;
    @BindView(R.id.tv_member_name)
    TextView tvMemberName;
    @BindView(R.id.tv_phone)
    RTextView tvPhone;
    @BindView(R.id.tv_person_num)
    TextView tvPersonNum;
    @BindView(R.id.tv_remark)
    TextView tvRemark;
    @BindView(R.id.tv_btn_2)
    SuperButton tvBtn2;
    @BindView(R.id.tv_btn_3)
    SuperButton tvBtn3;
    @BindView(R.id.ll_button)
    LinearLayout llButton;
    @BindView(R.id.tv_yy_time)
    TextView tvYyTime;


    private ServiceGoodsOrderInfo serviceGoodsOrderInfo;
    private int doType = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        setContentView(R.layout.activity_service_order_manager_detail);
        ButterKnife.bind(this);
        MyActivityManager.getInstance().addActivity(this);
        initView();

        initData();
    }

    private void initData() {
        tvOrderNo.setText(StringUtils.isBlank(serviceGoodsOrderInfo.getOrder_no()) ? "" : "订单号: " + serviceGoodsOrderInfo.getOrder_no());
        tvOrderTime.setText(StringUtils.isBlank(serviceGoodsOrderInfo.getOrder_time()) ? "" : "订单时间: " + serviceGoodsOrderInfo.getOrder_time());
        tvOrderNum.setText("共" + serviceGoodsOrderInfo.getOrder_num() + "件商品");
        tvOrderAmt.setText(StringUtils.formatIntMoney(serviceGoodsOrderInfo.getOrder_amt()));
        tvMemberName.setText(StringUtils.isBlank(serviceGoodsOrderInfo.getMemberName()) ? "" : serviceGoodsOrderInfo.getMemberName());
        tvPhone.setText(StringUtils.isBlank(serviceGoodsOrderInfo.getShopBooKingMessage().getPhone()) ? "" : serviceGoodsOrderInfo.getShopBooKingMessage().getPhone());
        tvPhone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DialogUtils.alert("拨打 " + serviceGoodsOrderInfo.getShopBooKingMessage().getPhone(), null, mContext, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + serviceGoodsOrderInfo.getShopBooKingMessage().getPhone()));
                        // intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        mContext.startActivity(intent);
                    }
                }, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();

                    }
                });

            }
        });

        Date date = TimeUtil.getDateFromString(serviceGoodsOrderInfo.getShopBooKingMessage().getBookingDate(), "yyyy-MM-dd");
        String dateStr = TimeUtil.getStringFromDate(date, "yyyy-MM-dd") + " "+serviceGoodsOrderInfo.getShopBooKingMessage().getBookingTime();
        tvYyTime.setText(dateStr);
        tvPersonNum.setText(serviceGoodsOrderInfo.getShopBooKingMessage().getPersonCount() + "人");
        tvRemark.setText(StringUtils.isBlank(serviceGoodsOrderInfo.getShopBooKingMessage().getBookingMessage()) ? "暂无" : serviceGoodsOrderInfo.getShopBooKingMessage().getBookingMessage());


        if (serviceGoodsOrderInfo.getIs_verify() == 0) {
            tvBtn2.setVisibility(View.VISIBLE);
            tvBtn3.setVisibility(View.VISIBLE);
            tvBtn3.setText("确认预约");
            tvBtn3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
//                    listener.commit(data.getOrder_id(), data.getShopBooKingMessage().getMemberId(), 1, serviceGoodsOrderInfo.getOrder_no());

                    MyDialog.Builder builder = new MyDialog.Builder(mContext);
                    builder.setTitle("提示");
                    builder.setMessage("确定预约订单?");
                    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                            doType = 1;
                            doCommint(serviceGoodsOrderInfo.getOrder_id(), 1);
                        }
                    });
                    builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int arg1) {
                            dialog.dismiss();
                        }
                    });
                    builder.create().show();


                }
            });
            tvBtn2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
//                    listener.commit(data.getOrder_id(), data.getShopBooKingMessage().getMemberId(),2, serviceGoodsOrderInfo.getOrder_no());
                    MyDialog.Builder builder = new MyDialog.Builder(mContext);
                    builder.setTitle("提示");
                    builder.setMessage("确定拒绝预约订单？");
                    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                            doType = 2;
                            doRefundCommint(serviceGoodsOrderInfo.getOrder_id(), serviceGoodsOrderInfo.getShopBooKingMessage().getMemberId(), serviceGoodsOrderInfo.getOrder_no(), doType);

                        }
                    });
                    builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int arg1) {
                            dialog.dismiss();
                        }
                    });
                    builder.create().show();
                }
            });
        } else if (serviceGoodsOrderInfo.getIs_verify() == 1) {
            tvBtn2.setVisibility(View.GONE);
            tvBtn3.setVisibility(View.VISIBLE);
            tvBtn3.setText("取消预约");
            tvBtn3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
//                    listener.refund(serviceGoodsOrderInfo.getOrder_id(), serviceGoodsOrderInfo.getShopBooKingMessage().getMemberId(), serviceGoodsOrderInfo.getOrder_no());
                    MyDialog.Builder builder = new MyDialog.Builder(mContext);
                    builder.setTitle("提示");
                    builder.setMessage("确定拒绝预约订单？");
                    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                            doType = 4;
                            doRefundCommint(serviceGoodsOrderInfo.getOrder_id(), serviceGoodsOrderInfo.getShopBooKingMessage().getMemberId(), serviceGoodsOrderInfo.getOrder_no(), doType);

                        }
                    });
                    builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int arg1) {
                            dialog.dismiss();
                        }
                    });
                    builder.create().show();

                }
            });
        } else {
            llButton.setVisibility(View.GONE);
        }


        //商品列表
        if (serviceGoodsOrderInfo.getGood_list() != null && serviceGoodsOrderInfo.getGood_list().size() > 0) {

            listviewSpec.setAdapter(new ServiceOrderListAdapter(mContext, serviceGoodsOrderInfo.getGood_list()));

        } else {
            listviewSpec.setVisibility(View.GONE);
        }
    }

    private void initView() {
        RelativeLayout relativeLayout = findViewById(R.id.lv_heard);
        relativeLayout.setBackgroundColor(getResources().getColor(R.color.statusBar));
        showView(R.id.titleback_btn_back, true);
        setTvText(R.id.tv_title, "订单详情 ");
        showView(R.id.next_sure, false);


        serviceGoodsOrderInfo = (ServiceGoodsOrderInfo) getIntent().getSerializableExtra("data");


    }

    private void doCommint(long id, int type) {
        String url = Config.SBS_ORDER_SERVICE_YY_VERIFY;
        RequestParams params = new RequestParams();
        params.put("isVerify", type);
        params.put("orderId", id);
        showProgress("加载中...");

        HttpUtil.get(url, params, new HttpUtil.RequestListener() {

            @Override
            public void success(String response) {
                disShowProgress();
                try {


                    JSONObject result = new JSONObject(response);
                    if (!result.optBoolean("status")) {
                        showErrorMsg(result.getString("message"));
                        return;
                    }

                    String message = "";
                    if (doType == 1){
                        message = "确认订单成功";
                    }else if (doType == 2){
                        message = "预约拒绝成功";
                    }else if (doType == 3){
                        message = "取消预约成功";
                    }


                    MyDialog.Builder builder = new MyDialog.Builder(mContext);
                    builder.setTitle("提示");
                    builder.setMessage(message);
                    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                            finish();

                        }
                    });
                    builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int arg1) {
                            dialog.dismiss();
                            finish();
                        }
                    });
                    builder.create().show();

                } catch (JSONException e) {
                    e.printStackTrace();

                }
            }

            @Override
            public void failed(Throwable error) {

                showE404();
                disShowProgress();
            }
        });

    }


    private void doRefundCommint(final long id, long memberId, String orderNo, final int type) {
        String url = Config.SBS_ORDER_SERVICE_REFUND;
        RequestParams params = new RequestParams();
        params.put("memberId", memberId);
        params.put("orderId", orderNo);
        showProgress("加载中...");

        HttpUtil.post(url, params, new HttpUtil.RequestListener() {

            @Override
            public void success(String response) {
                disShowProgress();
                try {


                    JSONObject result = new JSONObject(response);
                    if (!result.optBoolean("status")) {
                        showErrorMsg(result.getString("message"));
                        return;
                    }
//					refreshData();

                    doCommint(id, type);


                } catch (JSONException e) {
                    e.printStackTrace();

                }
            }

            @Override
            public void failed(Throwable error) {

                showE404();
                disShowProgress();
            }
        });

    }












    @Override
    protected void onResume() {
        super.onResume();
    }


    @OnClick({R.id.titleback_btn_back, R.id.tv_btn_2, R.id.tv_btn_3})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.titleback_btn_back:
                finish();
                break;
            case R.id.tv_btn_2:
                break;
            case R.id.tv_btn_3:
                break;
        }
    }
}
